﻿
using Newtonsoft.Json;
using NewZealandCoffee.model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;

namespace NewZealandCoffee
{

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static bool isMake = true;
        public static double MakingTypes = 0;
        public static string img_path = "";
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessage.txt";
        public static string WarnPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Warn";
        public static double GJIint1 = 1;
        public static double GJIint2 = 1;
        public static double GJIint3 = 1;
        public static double GSIint1 = 1;
        public static double GSIint2 = 1;
        public static double BSIint1 = 1;
        public static int IceIint1 { get; set; } = 1;
        public static int IceIint2 { get; set; } = 1;
        public static int IceIint3 { get; set; } = 1;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.RobotIPRight = porPortInfo.RobotIPRight;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.PrinterCoffeeIP = porPortInfo.PrinterCoffeeIP;
                    IPorPortInfo.Instance.PrinterCofferPort = porPortInfo.PrinterCofferPort;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                    IPorPortInfo.Instance.GJIint1 = porPortInfo.GJIint1;
                    IPorPortInfo.Instance.GJIint2 = porPortInfo.GJIint2;
                    IPorPortInfo.Instance.GJIint3 = porPortInfo.GJIint3;
                    IPorPortInfo.Instance.GSIint1 = porPortInfo.GSIint1;
                    IPorPortInfo.Instance.GSIint2 = porPortInfo.GSIint2;
                    IPorPortInfo.Instance.BSIint1 = porPortInfo.BSIint1;
                    GJIint1 = porPortInfo.GJIint1;
                    GJIint2 = porPortInfo.GJIint2;
                    GJIint3 = porPortInfo.GJIint3;
                    GSIint1 = porPortInfo.GSIint1;
                    GSIint2 = porPortInfo.GSIint2;
                    BSIint1 = porPortInfo.BSIint1;
                    IPorPortInfo.Instance.GJIint1 = porPortInfo.GJIint1;
                    IPorPortInfo.Instance.GJIint2 = porPortInfo.GJIint2;
                    IPorPortInfo.Instance.GJIint3 = porPortInfo.GJIint3;
                    IPorPortInfo.Instance.GSIint1 = porPortInfo.GSIint1;
                    IPorPortInfo.Instance.GSIint2 = porPortInfo.GSIint2;
                    IPorPortInfo.Instance.BSIint1 = porPortInfo.BSIint1;
                    WuLiaoIceSel.Instance.SugarInit1 = porPortInfo.GJIint1;
                    WuLiaoIceSel.Instance.SugarInit2 = porPortInfo.GJIint2;
                    WuLiaoIceSel.Instance.SugarInit3 = porPortInfo.GJIint3;
                    WuLiaoIceSel.Instance.GSInit1 = porPortInfo.GSIint1;
                    WuLiaoIceSel.Instance.GSInit2 = porPortInfo.GSIint2;
                    WuLiaoIceSel.Instance.BSInit1 = porPortInfo.BSIint1;
                }
                Thread thr3 = new Thread(SocketClients.Instance.socketclient);
                thr3.IsBackground = true;
                thr3.Start();

                if (!Directory.Exists(WarnPath))
                {
                    Directory.CreateDirectory(WarnPath);
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                WarnPath = WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                //Thread thr4 = new Thread(EWMMsg.Instance.IOSerial_Thr);
                //thr4.IsBackground = true;
                //thr4.Start();
                //Thread thr5 = new Thread(PwebSockets.Instance.Printer_Thr);
                //thr5.IsBackground = true;
                //thr5.Start();
                //Thread thr6 = new Thread(MessageShow);
                //thr6.IsBackground = true;
                //thr6.Start();
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 全部订单查询
        /// </summary>
        public static void MessageShow()
        {
            while (true)
            {
                try
                {
                    if ((!WuLiaoIceSel.Instance.IceCup1_Bol || !WuLiaoIceSel.Instance.IceCup2_Bol) && WuLiaoIceSel.Instance.CupFeed2_Sel_Bol)
                    {
                        RobotOperate.Instance.CX_Start();
                        Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                        if (RobotOperate.Instance.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                        {
                            CoffeeCupMaking();
                        }

                    }
                    else
                    {
                        Device device = new Device();
                        string lists = JsonConvert.SerializeObject(device);
                        var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);

                        if (data != null)
                        {
                            if (isMake)
                            {
                                var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                                if (msglist != null)
                                {
                                    if (msglist.data.ToString().Length > 10)
                                    {
                                        var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                        if (list.Count > 0)
                                        {
                                            if (isMake)
                                            {
                                                if (list.Count > 0)
                                                {
                                                    var ddd = list.FirstOrDefault(a => a.typeList3.Any(b => b.status == "3" || b.status == "4"));
                                                    if (ddd != null)
                                                    {
                                                        var make = ddd.typeList3.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                        //var ddd = list.FirstOrDefault(a => a.typeList3.Count > 0);
                                                        //if (ddd != null)
                                                        //{
                                                        //    if (ddd.typeList3.Count > 0)
                                                        //    {
                                                        //  var make = list[0].typeList3.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                        if (make != null)
                                                        {
                                                            if (make.jsonCodeVal.Length > 4)
                                                            {
                                                                var coffeecode = JsonConvert.DeserializeObject<List<specification>>(make.jsonCodeVal);
                                                                if (coffeecode.Count > 0)
                                                                {
                                                                    var coffeeID = coffeecode.FirstOrDefault(a => a.classCode != "");
                                                                    if (coffeeID != null)
                                                                    {
                                                                        //RobotOperate.Instance
                                                                        RobotOperate.Instance.CX_Start();
                                                                        Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                                        if (RobotOperate.Instance.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                                        {
                                                                            CoffeeMaking(coffeeID.classCode, make, ddd.orderNum);
                                                                            // CoffeeMaking("3101", null, "");
                                                                        }
                                                                    }
                                                                }
                                                            }

                                                        }
                                                    }
                                                    //else
                                                    //{
                                                    //    var make1 = list[0].typeList4.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                    //    if (make1 != null)
                                                    //    {
                                                    //        if (make1.jsonCodeVal.Length > 4)
                                                    //        {
                                                    //            var coffeecode = JsonConvert.DeserializeObject<List<specification>>(make1.jsonCodeVal);
                                                    //            if (coffeecode.Count > 0)
                                                    //            {
                                                    //                var coffeeID = coffeecode.FirstOrDefault(a => a.classCode != "");
                                                    //                if (coffeeID != null)
                                                    //                {
                                                    //                    RobotOperate.Instance.CX_Start();
                                                    //                    Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                    //                    if (RobotOperate.Instance.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                    //                    {
                                                    //                        CoffeeMaking(coffeeID.classCode, make1, list[0].orderNum);
                                                    //                        //  CoffeeMaking("", null, "");
                                                    //                    }
                                                    //                }
                                                    //            }
                                                    //        }
                                                    //    }
                                                    //}
                                                    //}
                                                    // }
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                        }
                        Thread.Sleep(1000);
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    //return;
                    Thread.Sleep(1000);
                }
            }

        }
        public static void CoffeeCupMaking()
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB3_UP);
                int cupCode = !WuLiaoIceSel.Instance.IceCup1_Bol ? 1 : 2;
                QBMsg12();
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                Thread.Sleep(200);
                while (true)
                {
                    if (WuLiaoIceSel.Instance.TakeCup2_Sel_Bol)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBCG3_UP);
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(cupCode == 1 ? DigitalMsg.Instance.LRobot_QFB1_UP : DigitalMsg.Instance.LRobot_QFB2_UP);
                        Thread.Sleep(200);
                        IOMsg.Instance.AllBol = true;
                        break;
                    }
                    else
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJC3_Down);
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBSB3_UP);
                        Thread.Sleep(200);
                        IOMsg.Instance.AllBol = true;
                        Thread.Sleep(200);
                        QBMsg12();
                        IOMsg.Instance.AllBol = true;
                        Thread.Sleep(200);
                    }
                }
                while (cupCode == 1 ? !WuLiaoIceSel.Instance.IceCup1_Bol : !WuLiaoIceSel.Instance.IceCup2_Bol) { Thread.Sleep(10); }
                IOMsg.Instance.QKCoffee();
            }
            catch (Exception)
            {
                return;
            }
        }
        public static void QBMsg12()
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                while (true)
                {
                    Console.WriteLine("daojiebeidian1 " + DateTime.Now.ToString());
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDJBD3);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB3_Down);
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBSB3_Down);
                        Thread.Sleep(200);
                        break;
                    }
                }
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_start);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                while (WuLiaoIceSel.Instance.LBQFK2_Bol) { Thread.Sleep(10); }
                while (!WuLiaoIceSel.Instance.LBQFK2_Bol) { Thread.Sleep(10); }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(500);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_end);
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJC3_UP);
                Thread.Sleep(200);
                IOMsg.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDJCD3);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                }
                IOMsg.Instance.AllBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 取杯
        /// </summary>
        /// <param name="cupCode"></param>
        public static void QBMsg(string code)
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                while (true)
                {
                    Console.WriteLine("daojiebeidian1 " + DateTime.Now.ToString());
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDLBQ);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJ_Down);
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB_Down);
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBSB_Down);
                        Thread.Sleep(200);
                        break;
                    }
                }
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup1_start);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                while (WuLiaoIceSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                while (!WuLiaoIceSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup1_end);
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJC_UP);
                Thread.Sleep(200);
                IOMsg.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDJCD);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                }
                IOMsg.Instance.AllBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        public static void CoffeeMaking(string makecoffeeName, DeviceOrderQueue list, string orderNum, bool CoffeeBol = true)
        {
            try
            {
                //if (!coffeeMsg.Instance.isMaking)
                //{
                //    isMake = true;
                //    return;
                //}
                //if (!WuLiaoIceSel.Instance.CoffeeBean_Sel_Bol || !WuLiaoIceSel.Instance.Milk_Sel_Bol || !WuLiaoIceSel.Instance.Water_Sel_Bol || !WuLiaoIceSel.Instance.CupFeed_Sel_Bol)
                //{
                //    isMake = true;
                //    return;
                //}
                isMake = false;
                if (!WuLiaoIceSel.Instance.PushRodUP_Sel_Bol)
                {
                    CoffeeMakes.Instance.MakeMealMsg(orderNum);//出餐
                }
                //isMake = false;
                int cupCode = 0;
                int sugarCode = 0;//果酱
                int GSCode = 0;//果碎
                string sauceCode = "";
                string nutletCode = "";
                var msgs = JsonConvert.DeserializeObject<List<specification>>(list.jsonCodeVal);
                if (msgs != null)
                {
                    if (msgs.Any(a => a.JamCode != ""))
                    {
                        sugarCode = msgs.Any(a => a.JamCode == "1") ? 1 : msgs.Any(a => a.JamCode == "2") ? 2 : msgs.Any(a => a.JamCode == "3") ? 3 : 0;
                    }

                    if (msgs.Any(a => a.CrushedFruitCode != ""))
                    {
                        GSCode = msgs.Any(a => a.CrushedFruitCode == "1") ? 1 : msgs.Any(a => a.CrushedFruitCode == "2") ? 2 : 0;
                    }
                    //  sugarCode = makecoffeeName == "3104" ? 3 : makecoffeeName == "3103" ? 2 : makecoffeeName == "3102" ? 1 : 0;// msgs.Any(a => a.GJCode == "11") ? 1 : msgs.Any(a => a.GJCode == "12") ? 2 : msgs.Any(a => a.GJCode == "13") ? 3 : 0;
                    // GSCode = msgs.Any(a => a.XLCode == "15") ? 1 : msgs.Any(a => a.XLCode == "16") ? 2 : 0;
                }
                while (true)
                {
                    EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                    status.orderGoodsId = list.id;
                    status.orderId = list.orderId;
                    status.status = 4;
                    string lists = JsonConvert.SerializeObject(status);
                    var ddd1 = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                    if (ddd1 != null)
                    {
                        if (ddd1 != "")
                        {
                            var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                            if (list10 != null)
                            {
                                if (list10.code == "0" || list10.code == "1")
                                {
                                    Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                    break;
                                }
                            }
                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                        }

                    }
                    Thread.Sleep(100);
                }
                if (CoffeeBol)
                {
                    IOMsg.Instance.AllBol = false;
                    if (sugarCode == 1)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GJ1_UP);
                    }
                    else if (sugarCode == 2)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GJ2_UP);
                    }
                    else if (sugarCode == 3)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GJ3_UP);
                    }
                    if (GSCode == 1)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GS1_UP);
                    }
                    else if (GSCode == 2)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GS2_UP);
                    }
                    if (makecoffeeName == "3103" || makecoffeeName == "3102" || makecoffeeName == "3101")
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_BQL_UP);
                    }
                    else if (makecoffeeName == "3002")
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XN1_UP);
                    }
                    else if (makecoffeeName == "3003")
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XN2_UP);
                    }
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJ_UP);
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB_UP);
                    Thread.Sleep(100);
                    QBMsg(makecoffeeName);
                    Thread.Sleep(200);
                    IOMsg.Instance.AllBol = true;
                    Thread.Sleep(200);
                    while (true)
                    {
                        if (WuLiaoIceSel.Instance.TakeCup_Sel_Bol)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBWC_UP);
                            Thread.Sleep(200);
                            break;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBSB_UP);
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            Thread.Sleep(200);
                            QBMsg(makecoffeeName);
                            IOMsg.Instance.AllBol = true;
                            Thread.Sleep(200);
                        }
                    }
                }
                IOMsg.Instance.AllBol = false;
                if (makecoffeeName == "3103" || makecoffeeName == "3102" || makecoffeeName == "3101")
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_BQL_Down);
                }
                else if (makecoffeeName == "3002")
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XN1_Down);
                }
                else if (makecoffeeName == "3003")
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XN2_Down);
                }
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                // Thread.Sleep(1000);
                if (makecoffeeName == "3103" || makecoffeeName == "3102" || makecoffeeName == "3101")
                    CoffeeMakes.Instance.BQLMaking(makecoffeeName);
                if (makecoffeeName == "3103" || makecoffeeName == "3102" || makecoffeeName == "3101")
                {
                    if (sugarCode != 0)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOMsg.Instance.RobotBol = false;
                        while (true)
                        {
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDGJD);
                            Thread.Sleep(100);
                            if (IOMsg.Instance.RobotBol)
                            {
                                //IOMsg.Instance.QK();
                                Thread.Sleep(200);
                                break;
                            }
                        }
                    }
                    if (sugarCode == 1)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ1_start);
                        Thread.Sleep(Convert.ToInt32(GJIint1 * 1000));
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ1_end);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GJ1_Down);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                    }
                    else if (sugarCode == 2)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ2_start);
                        Thread.Sleep(Convert.ToInt32(GJIint2 * 1000));
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ2_end);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GJ2_Down);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                    }
                    else if (sugarCode == 3)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ3_start);
                        Thread.Sleep(Convert.ToInt32(GJIint3 * 1000));
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GJ3_end);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GJ3_Down);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                    }
                    if (sugarCode != 0)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGJWC_UP);
                        Thread.Sleep(100);
                    }
                    if (GSCode != 0)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOMsg.Instance.RobotBol = false;
                        while (true)
                        {
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDGSD);
                            Thread.Sleep(100);
                            if (IOMsg.Instance.RobotBol)
                            {
                                //IOMsg.Instance.QK();
                                Thread.Sleep(200);
                                break;
                            }
                        }
                        Thread.Sleep(100);
                    }
                    //  IOMsg.Instance.AllBol = true;
                    if (GSCode == 1)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GS1_start);
                        Thread.Sleep(Convert.ToInt32(GSIint1 * 1000));
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GS1_end);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GS1_Down);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                    }
                    else if (GSCode == 2)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GS2_start);
                        Thread.Sleep(Convert.ToInt32(GSIint2 * 1000));
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GS2_end);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_GS2_Down);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                    }
                    if (GSCode != 0)
                    {
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGSWC_UP);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                    }
                }
                /// CoffeeMakes.Instance.CoffeeMaking(makecoffeeName, list.lhImgPath);
                // CoffeeMakes.Instance.CoffeeMaking("", "");
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                IOMsg.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(100);
                    if (makecoffeeName == "3103" || makecoffeeName == "3102" || makecoffeeName == "3101")
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JFWC);
                    else
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XNJFWC);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {

                        Thread.Sleep(200);
                        break;

                    }
                }
                CoffeeMakes.Instance.MakeMealMsg(orderNum);//出餐
                IOMsg.Instance.QK();
                //ClientList clientList1 = new ClientList();
                //clientList1.MsgBol = WuLiaoIceSel.Instance;
                //clientList1.code = 10;
                //clientList1.IsMake = false;
                //SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList1)));
                IOMsg.Instance.AllBol = true;
                Thread.Sleep(200);
                GC.Collect();
                while (true)
                {
                    EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                    status.orderGoodsId = list.id;
                    status.orderId = list.orderId;
                    status.status = 5;
                    string lists = JsonConvert.SerializeObject(status);
                    var ddd1 = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                    if (ddd1 != null)
                    {
                        if (ddd1 != "")
                        {
                            var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                            if (list10 != null)
                            {
                                if (list10.code == "0" || list10.code == "1")
                                {
                                    Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                    break;
                                }
                            }
                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                        }

                    }
                    Thread.Sleep(100);
                }
                //EditDeviceOrderStatus editDeviceOrder2 = new EditDeviceOrderStatus();
                //editDeviceOrder2.status = 5;
                //editDeviceOrder2.orderId = list.orderId;
                //editDeviceOrder2.orderGoodsId = list.id;
                //var ddd2 = JsonConvert.SerializeObject(editDeviceOrder2);
                //HttpMsg.Instance.editDeviceOrderStatusMsg(ddd2);
                isMake = true;
            }
            catch (Exception)
            {
                return;
            }
        }
    }
    public class ListMsg
    {
        //"code":0,"msg":"Operation successfully","data":[]
        public string code { get; set; }
        public object msg { get; set; }
        public object data { get; set; }
    }
}
